package org.tigr.htc.cmd;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.io.Serializable;

import org.apache.log4j.Logger;

/**
 * The <b>CommandConfig</b> holds all the configuration variables 
 * for a command.  It is a simple struct for holding command config 
 * information and doesn't do any real work.
 *
 */

public class CommandConfig implements Serializable {

	private static final long serialVersionUID = 7578019315974414870L;

	static Logger log = Logger.getLogger(CommandConfig.class);
    
    //final static String DEF_OPSYS = "LINUX,SOLARIS";
	// Changing the default operating system to null
	final static String DEF_OPSYS = null;
    final static String DEF_LENGTH = "medium";
    
    // configuration variables
    // comma seperated list of operating systems (LINUX,SOLARIS,OSF)
    protected String opsys = null;
    // comma seperated list of hosts (firecoral,shrew)
    protected String hosts;
    // pass through config value can be set to anything
    
    protected String logLocation;
    protected String passThrough;
    protected String length;
    protected int memory;
    protected int priority;
    protected long runningTime = 0;
    
    // Default value set to true, so that all the requests that come form 
    // Java HTC request are set to evictable, by deault.
    protected boolean evictable = true;
    protected String cmdClass = null;
    protected boolean getenv;
    //private Command cmd;
    protected Command cmd;
    private int id;
    
    
    /**
     * Creates a new <code>CommandConfig</code> instance and 
     * set default variables.
     */
    public CommandConfig() {
        opsys = DEF_OPSYS;
        length = DEF_LENGTH;
        getenv = false;
        memory = 0;  // set no memory limit
        logLocation = new String();
        passThrough = new String();
        cmdClass = new String();
    }
    
    /**
     * This constructor creates a new instance.
     *  
     * @param opsys
     * @param hosts
     * @param logLocation
     * @param passThrough
     * @param length
     * @param memory
     * @param priority
     * @param cmdClass
     * @param getenv
     */
    public CommandConfig(String opsys, String hosts, String logLocation, String passThrough, String length,
            int memory, int priority, String cmdClass, boolean getenv) {
        super();
        this.opsys = opsys;
        this.hosts = hosts;
        this.logLocation = logLocation;
        this.passThrough = passThrough;
        this.length = length;
        this.memory = memory;
        this.priority = priority;
        this.cmdClass = cmdClass;
        this.getenv = getenv;
    }
    
    /**
     * <code>toString</code> method returns a xml string 
     * representation of this commandconfig.
     *
     * @return a <code>String</code> value
     */
    public String toString() {
        return CommandHelper.getInstance().marshal(this);
    }
    
    //
    // Getters & Setters
    //
    
    /**
     * Get the Memory value.
     * @return the Memory value.
     */
    public int getMemory() {
        return memory;
    }
    
    /**
     * Set the Memory value.
     * @param p_Memory The new Memory value.
     */
    public void setMemory(int p_memory) {
        memory = p_memory;
    }
    
    /**
     * <code>getID</code> get command ID.
     * 
     * @return an <code>long</code> command id
     */
    public int getID() {
	log.debug(" config get id ->" + id);
	return this.id;
    }
    
    /**
     * <code>setID</code> set command ID.
     * 
     * @param p_id
     *            an <code>long</code> command id
     */
    public void setID(int p_id) {
	log.debug("config set id " + p_id);
	this.id = p_id;
    }

    /**
     * Gets the value of opsys
     *
     * @return the value of opsys
     */
    public String getOpSys()  {
        return opsys;
    }
    
    /**
     * Sets the value of opsys
     *
     * @param p_Opsys Value to assign to this.opsys
     */
    public void setOpSys(String p_opsys) {
        this.opsys = p_opsys;
    }
    
    
    /**
     * Gets the value of hosts
     *
     * @return the value of hosts
     */
    public String getHosts()  {
        return hosts;
    }
    
    /**
     * Sets the value of hosts
     *
     * @param p_hosts Value to assign to this.hosts
     */
    public void setHosts(String p_hosts) {
        this.hosts = p_hosts;
    }
    
    /**
     * Gets the value of logLocation
     *
     * @return the value of logLocation
     */
    public String getLogLocation()  {
        return logLocation;
    }
    
    /**
     * Sets the value of logLocation
     *
     * @param p_logLocation Value to assign to this.logLocation
     */
    public void setLogLocation(String p_logLocation) {
        this.logLocation = p_logLocation;
    }
    
    /**
     * Gets the value of passThrough
     *
     * @return the value of passThrough
     */
    public String getPassThrough()  {
        return passThrough;
    }
    
    /**
     * Sets the value of passThrough
     *
     * @param p_passThrough Value to assign to this.passThrough
     */
    public void setPassThrough(String p_passThrough) {
        this.passThrough = p_passThrough;
    }
    
    /**
     * Gets the value of length
     *
     * @return the value of length
     */
    public String getLength()  {
        return this.length;
    }
    
    /**
     * Sets the value of length
     *
     * @param p_Length Value to assign to this.length
     */
    public void setLength(String p_length) {
        this.length = p_length;
    }
    
    
    /**
     * Gets the value of priority
     *
     * @return the value of priority
     */
    public int getPriority()  {
        return this.priority;
    }
    
    /**
     * Sets the value of priority
     *
     * @param p_Priority Value to assign to this.priority
     */
    public void setPriority(int p_priority) {
        this.priority = p_priority;
    }
    
    /**
     * Gets the value of cmdClass
     *
     * @return the value of cmdClass
     */
    public String getCmdClass()  {
        return this.cmdClass;
    }
    
    /**
     * Sets the value of cmdClass
     *
     * @param p_CmdClass Value to assign to this.cmdClass
     */
    public void setCmdClass(String p_CmdClass) {
        this.cmdClass = p_CmdClass;
    }
    
    /**
     * Gets the value of getenv
     *
     * @return the value of getenv
     */
    public boolean isGetenv()  {
        return this.getenv;
    }
    
    /**
     * Sets the value of getenv
     *
     * @param p_Getenv Value to assign to this.getenv
     */
    public void setGetenv(boolean p_getenv) {
        this.getenv = p_getenv;
    }

    public Command getCommand() {
	log.debug("commandConfig get command ->" + cmd.getID());
	return this.cmd;
    }
    
    public void setCommand(Command p_cmd) {
	log.debug("commandConfig set command " + p_cmd.getID());
	this.cmd = p_cmd;
    }
    
    public Object clone () {
        return new CommandConfig(opsys, hosts, logLocation, passThrough, length,
				 memory, priority, cmdClass, getenv);
    }

	public long getRunningTime() {
		return runningTime;
	}

	public void setRunningTime(long runningTime) {
		this.runningTime = runningTime;
	}

	public boolean isEvictable() {
		return this.evictable;
	}

	public void setEvictable(boolean evictable) {
		this.evictable = evictable;
	}

}
